<template>
  <div class="shopcar-container">
    <!-- 商品区域 -->
    <div class="goods-list">
      <div class="mui-card" v-for="(item, i) in shopCarList" :key="item.id">
        <div class="mui-card-content">
          <div class="mui-card-content-inner">
            <mt-switch
              v-model="$store.getters.getGoodsSelected[item.id]"
              @change="selectedChanged(item.id, $store.getters.getGoodsSelected[item.id])"
            ></mt-switch>
            <img :src="item.thumb_path" alt="图片加载失败了~" />
            <div class="info">
              <h3>{{ item.title }}</h3>
              <p>
                <span class="price">￥{{ item.sell_price }}</span>
                <numbox :initcount="$store.getters.getGoodsCount[item.id]" :goodsId="item.id"></numbox>
                <a href="javascript:;" @click.prevent="remove(item.id, i)">删除</a>
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 结算区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner jiesuan">
          <div class="left">
            <p class="count">总计(不含运费)</p>
            <p>
              已勾选商品
              <span class="red">{{ $store.getters.getGoodsCountAndAmount.count }}</span> 件，
              总价：
              <span
                class="red"
              >￥{{ $store.getters.getGoodsCountAndAmount.amount }}</span>
            </p>
          </div>
          <mt-button type="danger">结算</mt-button>
        </div>
      </div>
    </div>
    <p>{{ $store.getters.getGoodsSelected }}</p>
  </div>
</template>

<script>
import numbox from "../common/shopCar_numbox.vue";
export default {
  data() {
    return {
      shopCarList: []
    };
  },
  methods: {
    // 获取购物车中的商品列表
    getShopCarList() {
      // 获取 store 中 car 所有的 id
      var idArr = [];
      this.$store.state.car.forEach(item => {
        idArr.push(item.id);
      });
      if (idArr.length <= 0) {
        return;
      }

      this.$http
        .get("api/goods/getshopcarlist/" + idArr.join(","))
        .then(result => {
          if (result.body.status === 0) {
            this.shopCarList = result.body.message;
          }
        });
    },
    remove(id, index) {
      this.shopCarList.splice(index, 1);
      this.$store.commit("removeFormCar", id);
    },
    selectedChanged(id, val) {
      this.$store.commit("updateGoodsSelected", { id, selected: val });
    }
  },
  created() {
    this.getShopCarList();
  },
  components: {
    numbox
  }
};
</script>

<style lang="scss" scoped>
.shopcar-container {
  background-color: #eee;
  overflow: hidden;
  .goods-list {
    .mui-card {
      width: 100%;
    }
    .mui-card-content-inner {
      display: flex;
      align-items: center;
      padding: 10px 10px 10px 5px;
    }
    margin: 0;
    padding: 0;
    img {
      width: 50px;
      height: 50px;
      margin: 3px;
    }
    .info {
      margin-left: 3px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      h3 {
        font-size: 13px;
      }
      p {
        margin: 8px 0 0 0;
      }
      .price {
        color: red;
        font-weight: bold;
      }
    }
  }
  .jiesuan {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .red {
      color: red;
      font-weight: 700;
    }
  }
}
</style>